package com.ruoxi.blog.test;

/**
	* @author YaoXian
	* @title: Heap
	* @projectName ruoxi-blog
	* @description: 若兮微服务 -
	* @date 2021-04-17 15:34
	*/
public class Heap {
				private static void max_heapify(int[] datas,int i){
						int l = 2*i+1;
						int	r  = 2*i+2;
						int h = datas.length;
						int largest = 0;
						if(l<h && (datas[l]>datas[i])){
								largest = l;
						} else {
								largest = i;
						}
						
						if(r<h && (datas[r]>datas[largest])){
								largest = r;
						}
						
						System.out.println("(left:)"+l+"(right:)"+r+"(largest:)"+largest+"(i:)"+i);
						if(largest!=i){
								int temp = datas[i];
								datas[i] = datas[largest];
								datas[largest] = temp;
								max_heapify(datas,largest);
						}
				}
				
				private static void buildMax_heap(int[] datas){
						int le = datas.length/2;
						for(int i=le;i>=0;i--){
								max_heapify(datas,i);
						}
				}
				
				public static void main(String[] args){
						int[] datas={8,4,3,6,5,7,9,10,2,8};
						
						buildMax_heap(datas);
						
						for(int i=0;i<datas.length;i++){
								System.out.print(datas[i]);
						}
						
				}
}
